/**
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.biofoco.p2p.services;

import java.util.Map;

public class BlastService extends AbstractService<String> {
	
	@Override
	public ServiceResult<String> execute(long taskID, Map<String,String> params) throws ServiceException {
		System.out.println("Blast service executed!!!");
		return new ServiceResult<String>(taskID, "OK");
	}

}

//
//private static BlastService ref = null;
//
//// VARIABLES
//private static Runtime runner = null;
//private static List<String> queue = Collections.synchronizedList(new ArrayList<String>());
//private static final Logger LOGGER = Logger.getLogger(BlastService.class);	
//private Consumer consumer;
//
//private String address;
//private String username;
//private String passwd;
//private String filename;	
//
//// CONSTANTS
//private static final String LOCK = "BlastLock";
////private static final String inputDir  = "tmp/remote/input";
////private static final String outputDir = "tmp/remote/output";
//
//public BlastService() {
//	
//	ref = this;	
//	runner = Runtime.getRuntime();
//	consumer = new Consumer();
//	consumer.start();
//}
//
//private String downloadFile(String filename, String address, String username, String passwd) {
//	
//	// RETRIEVING INPUT FILES
//	
////	try {
////		SecureFTPClient client = new SecureFTPClient(username, passwd, address);
////		client.connect();
////		client.get(filename);
////		client.close();
////	} catch (SecureFTPException e) {
////		e.printStackTrace();
////	}
//	
//	return filename;
//}
//
//private String uploadFile (String filename, String address, String username, String passwd) {
//			
////	try {
////		
////		SecureFTPClient client = new SecureFTPClient(username, passwd, address);
////		client.connect();
////		client.put(filename, new FileInputStream(filename));
////		client.close();
////		
////	} catch (SecureFTPException e) {
////		e.printStackTrace();
////	} catch (FileNotFoundException e) {
////		e.printStackTrace();
////	}
//	
//	return filename;
//}	
//
////public synchronized String exec(XMLRequest document) {
//public synchronized String exec(String document) {		
//
//	LOGGER.debug("Executing BLAST locally.");
//	
////	address   = document.getFtpAddress();
////	username  = document.getFtpUser();
////	passwd    = document.getFtpPassword();
////	filename  = document.getFilename();
//			
//	String inFile = downloadFile(filename, address, username, passwd);
//	// PRODUCER STORES IN THE QUEUE
//	queue.add(inFile);
//	synchronized (LOCK) {
//		LOCK.notify();
//	}
//	
//	return "Job scheduled to be executed";
//}
//
//private void executeBLAST(String inFile) {	
//	
//	String inPath   = inFile;
//	String response = null;
//
//	String outPath  =  inFile + ".out";
//	String command  = "/home/edward/MyBlast.sh " + inPath + " " + outPath;
//	
//	try {
//		
//		Process proc = runner.exec(command);
//		InputStream is = proc.getInputStream();
//		StringBuilder buf = new StringBuilder();
//		int c;
//		while ((c = is.read()) != -1) {
//			buf.append((char) c);
//		}		
//		
//		response = "BLAST sucessfully executed!";
//		
//		uploadFile(outPath, address, username, passwd);
//	
//	} catch (IOException e) {
//		response = e.getMessage();
//	}
//	
//	LOGGER.debug(response);			
//}
//
///**
// * Consumer is in charge of really executing BLAST services as soon as there are at least one.
// * @author edward ribeiro (edwardribeiro@yahoo.com)
// *
// */
//private static class Consumer extends Thread {
//	public void run() {
//		  while (true) {
//			  synchronized (LOCK) {
//				  if (queue.isEmpty()) {				  
//					  try {
//						  LOCK.wait();
//					  } catch (InterruptedException e) {
//						  LOGGER.error(e.getMessage());
//					  }
//			  	  }
//				  else { 
//					  String filename = queue.remove(0);
//					  ref.executeBLAST(filename);
//				  }
//			  }		
//		  }			
//	}
//}
